{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to use Vanna with various databases\n",
    "\n",
    "You can use Vanna with any database that you can connect to via Python. Here are some examples of how to connect to various databases.\n",
    "\n",
    "All you have to do is provide Vanna with a function that takes in a SQL query and returns a Pandas DataFrame. Here are some examples of how to do that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vanna as vn"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **PostgreSQL**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import psycopg2\n",
    "\n",
    "conn_details = {...}  # fill this with your connection details\n",
    "conn_postgres = psycopg2.connect(**conn_details)\n",
    "\n",
    "def run_sql_postgres(sql: str) -> pd.DataFrame:\n",
    "    df = pd.read_sql_query(sql, conn_postgres)\n",
    "    return df\n",
    "\n",
    "vn.run_sql = run_sql_postgres"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Snowflake**\n",
    "\n",
    "We have a built-in function for Snowflake, so you don't need to write your own.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vn.connect_to_snowflake(account='my-account', username='my-username', password='my-password', database='my-database')\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Google BigQuery**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.cloud import bigquery\n",
    "import pandas as pd\n",
    "\n",
    "project_id = 'your-project-id'  # replace with your Project ID\n",
    "client_bigquery = bigquery.Client(project=project_id)\n",
    "\n",
    "def run_sql_bigquery(sql: str) -> pd.DataFrame:\n",
    "    df = client_bigquery.query(sql).to_dataframe()\n",
    "    return df\n",
    "\n",
    "vn.run_sql = run_sql_bigquery"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Amazon Athena**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pyathena import connect\n",
    "\n",
    "conn_details = {...}  # fill this with your connection details\n",
    "conn_athena = connect(**conn_details)\n",
    "\n",
    "def run_sql_athena(sql: str) -> pd.DataFrame:\n",
    "    df = pd.read_sql(sql, conn_athena)\n",
    "    return df\n",
    "\n",
    "vn.run_sql = run_sql_athena"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Amazon Redshift**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import psycopg2\n",
    "\n",
    "conn_details = {...}  # fill this with your connection details\n",
    "conn_redshift = psycopg2.connect(**conn_details)\n",
    "\n",
    "def run_sql_redshift(sql: str) -> pd.DataFrame:\n",
    "    df = pd.read_sql_query(sql, conn_redshift)\n",
    "    return df\n",
    "\n",
    "vn.run_sql = run_sql_redshift"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **Others**\n",
    "\n",
    "You can follow a similar pattern to the others for your database. You just have to provide a `vn.run_sql` function that takes in a SQL query and returns a Pandas DataFrame."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
